package com.sqsxblog.hybrid.shouldOverrideUrlLoading;

import android.content.Context;
import android.graphics.Bitmap;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**根据思路写的演示，不建议实际开发直接使用，如传参更建议使用json*/
public class ShouldOverrideUrlLoadingWebClient extends WebViewClient {

    //重定向用
    private boolean mIsRedirect;

    private Context context;

    public ShouldOverrideUrlLoadingWebClient(Context context)
    {
        this.context = context.getApplicationContext();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        mIsRedirect = true;
        //尝试获取
        String split[] = url.split("\\?");
        //格式：?方法名|参数1..
        if (split.length > 1)
        {
            //先获取
            String anchor = split[1];
            //根据参数格式化出参数
            split = anchor.split("\\|");
            String params[] = new String[split.length - 1];
            //数组下标0为方法名
            anchor = split[0];
            //根据方法名反射方法
            Class cls[] = new Class[params.length];
            for(int i = 1; i < split.length; i++)
            {
                cls[i - 1] = String.class;
                params[i - 1] = split[i];
            }
            try {
                Method method;
                if (cls.length == 0)
                {
                    method = getClass().getMethod(anchor);
                    if (!annotation(method)) return true;
                    method.invoke(this);
                }
                else
                {
                    method = getClass().getMethod(anchor,cls);
                    if (!annotation(method)) return true;
                    method.invoke(this,params);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else
        {
            view.loadUrl(url);
        }
        return true;
    }

    //判断是否有注解，如果没有注解就直接返回
    private boolean annotation(Method method)
    {
        try
        {
            Annotation annotation[] = method.getDeclaredAnnotations();
            for(int i = 0; i < annotation.length; i++)
            {
                if (annotation[i].toString().contains("JavascriptInterface"))
                {
                    return true;
                }
            }
        }
        catch (NoClassDefFoundError error)
        {
            //4.2以下没有JavascriptInterface注解，但可以通过error判断是否有JavascriptInterface注解
            //不过有个缺陷，如果非JavascriptInterface注解导致的错误，将判断不出来
            //建议自定义一个注解
            return error.getMessage().contains("JavascriptInterface");
        }
        return false;
    }

    @JavascriptInterface
    public void toast(String text)
    {
        Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
    }

    //无参数toast，也不提供注解，那么js将无法执行
    public void toast()
    {
        Toast.makeText(context,"无参数toast",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        mIsRedirect = false;
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (mIsRedirect) {
            return;
        }
    }
}
